package src.lab01;

public class Test05 {
    public static void main(String[] args) {
        int n = 100;
        System.out.println("for 循环求解下的结果=" + sum1(n));
        System.out.println("while 循环求解下的结果=" + sum2(n));
        System.out.println("do while 循环求解下的结果=" + sum3(n));
    }

    //for循环
    public static long sum1(int n) {
        int i = 0;
        long sum = 0;
        for (i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }

    //while 循环
    public static long sum2(int n) {
        int i = 0;
        long sum = 0;
        while (i <= n) {
            sum += i;
            i += 1;
        }
        return sum;
    }

    //do while循环
    public static long sum3(int n) {
        int i = 0;
        long sum = 0;
        do {
            sum += i;
            i++;
        } while (i <= n);
        return sum;
    }

}
